package cn.ezios.huo;

import cn.ezios.huo.system.biz.menu.MenuService;
import cn.ezios.huo.system.biz.menu.MenuVo;
import cn.ezios.huo.system.dao.domain.SysMenu;
import cn.ezios.huo.system.dao.service.SysMenuService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class SysMenuModuleServiceTEST {
    @Resource
    private MenuService menuService;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private ObjectMapper objectMapper;


    @Test
    public void createMenu() {
        sysMenuService
                .lambdaUpdate()
                .remove();
        log.info("开始创建菜单");
        String catalogPrefix = "目录CATA:";
        String menuPrefix = "菜单MENU:";
        for (int i = 0; i < 3; i++) {
            SysMenu catalog = menuService.creatMenuCatalog(catalogPrefix + i, menuService.MENU_TREE_ROOT);
            log.info("创建目录{}",catalog.getMenuName());
            int ran = (int) (Math.random() * 10);
//            int ran = RandomUtil.randomInt(10);
            for (int j = 0; j < ran; j++) {
                SysMenu menu = menuService.creatMenu(menuPrefix + i + "-" + j, catalog.getMenuId());
                log.info("创建菜单{}",menu.getMenuName());
            }
        }
        log.info("创建菜单结束");
    }

    @Test
    public void buildMenuTree() throws JsonProcessingException {
        List<SysMenu> list = sysMenuService.lambdaQuery()
//                .ge(SysMenu::getMenuId, "1813562191921213442")
//                .le(SysMenu::getMenuId, "1813562192374198274")
                .select(SysMenu::getMenuId, SysMenu::getMenuName)
                .list();
        String[] menuIds = list.stream().map(SysMenu::getMenuId).toArray(String[]::new);
        List<MenuVo> trees = menuService.getMenuTreeByMenuIds(menuIds);
        System.out.println(objectMapper.writeValueAsString(trees));
    }

    @Test
    public void listAllMenu() throws JsonProcessingException {
        List<SysMenu> list = sysMenuService.list();
        System.out.println(objectMapper.writeValueAsString(list));
    }
}
